package org.free.c3p0;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * User: 缪凯
 * Date: 12-1-10
 * Time: 上午10:59
 */
public class DBPool {
    public final static String DRIVER_FOR_MYSQL = "com.mysql.jdbc.Driver";
    public final static String SQL_11_X_5_URL= "jdbc:mysql://127.0.0.1:3306/lottery-analyzer?autoReconnect=true";
    public final static String SQL_11_X_5_USER= "root";
    public final static String SQL_11_X_5_PSD= "miaokai83";
    //********************************************连接池选项***********************************************
    //初始化时获取2个连接
    public final static int LA_POOL_SIZE_INIT = 2;

    //连接池中保留的最大连接数。Default: 5 maxPoolSize
    public final static int LA_POOL_SIZE_MAX = 5;

    //连接池中保留的最小连接数
    public final static int LA_POOL_SIZE_MIN = 1;

    //当连接池中的连接耗尽的时候c3p0一次同时获取的连接数
    public final static int LA_ACQ_INC = 1;

    //每60秒检查所有连接池中的空闲连接。
    public final static int LA_IDLE_CONN_TEST_PER = 60;

    ////最大空闲时间,2500秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0  maxIdleTime
    public final static int LA_IDLE_TIME_MAX = 2500;

    //连接关闭时默认将所有未提交的操作回滚
    public final static boolean LA_AUTO_COMMIT_ON_CLOSE = true;

    //定义所有连接测试都执行的测试语句
    public final static String LA_PRE_TEST_QUERY = "";

    //因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性
    public final static boolean LA_TEST_CONN_ON_CHECKOUT = false;

    //取得连接的同时将不校验连接的有效性
    public final static boolean LA_TEST_CONN_ON_CHECKIN = false;

    //在从数据库获取新连接失败后重复尝试的次数
    public final static int LA_ACQ_RETRY_TIMS = 10;

    //两次连接中间隔时间，单位毫秒
    public final static int LA_ACQ_RETRY_DELAY = 1000;

    //获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
    //保留，并在下次调用getConnection()的时候继续尝试获取连接。如果设为true，那么在尝试
    //获取连接失败后该数据源将申明已断开并永久关闭。Default: false  breakAfterAcquireFailure
    public final static boolean LA_BREAK_AFT_ACQ_FAILURE = true;



    private static DBPool FACTORY = null;
    private ComboPooledDataSource comboPooledDataSource = null;

    private DBPool() throws PropertyVetoException {
        this.comboPooledDataSource = new ComboPooledDataSource();

        this.comboPooledDataSource.setDriverClass(DBPool.DRIVER_FOR_MYSQL);
        this.comboPooledDataSource.setJdbcUrl(DBPool.SQL_11_X_5_URL);
        this.comboPooledDataSource.setUser(DBPool.SQL_11_X_5_USER);
        this.comboPooledDataSource.setPassword(DBPool.SQL_11_X_5_PSD);


        this.comboPooledDataSource.setInitialPoolSize(DBPool.LA_POOL_SIZE_INIT);
        this.comboPooledDataSource.setMaxPoolSize(DBPool.LA_POOL_SIZE_MAX);
        this.comboPooledDataSource.setMinPoolSize(DBPool.LA_POOL_SIZE_MIN);

        this.comboPooledDataSource.setAcquireIncrement(DBPool.LA_ACQ_INC);
        this.comboPooledDataSource.setIdleConnectionTestPeriod(DBPool.LA_IDLE_CONN_TEST_PER);

        this.comboPooledDataSource.setMaxIdleTime(DBPool.LA_IDLE_TIME_MAX);
        this.comboPooledDataSource.setAutoCommitOnClose(DBPool.LA_AUTO_COMMIT_ON_CLOSE);

        this.comboPooledDataSource.setTestConnectionOnCheckout(DBPool.LA_TEST_CONN_ON_CHECKOUT);
        this.comboPooledDataSource.setTestConnectionOnCheckin(DBPool.LA_TEST_CONN_ON_CHECKIN);

        this.comboPooledDataSource.setAcquireRetryAttempts(DBPool.LA_ACQ_RETRY_TIMS);
        this.comboPooledDataSource.setAcquireRetryDelay(DBPool.LA_ACQ_RETRY_DELAY);
        this.comboPooledDataSource.setBreakAfterAcquireFailure(DBPool.LA_BREAK_AFT_ACQ_FAILURE);
    }

    protected static DBPool initDBFactory() throws PropertyVetoException {
        if(FACTORY == null){
            FACTORY = new DBPool();
        }

        return FACTORY;
    }

    public Connection openConn() {
        try {
            return this.comboPooledDataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void closeConn(Connection conn) {
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
